package com.mork.action.order;

import java.util.ArrayList;
import java.util.List;

import com.mock.dao.MemberDao;
import com.mock.dao.OrderDao;
import com.mock.dao.OrderDetailDao;
import com.mock.dao.ProductDao;
import com.mock.dao.ProductTypeDao;
import com.mock.dao.UserDao;
import com.mock.dao.impl.MemberDaoImpl;
import com.mock.dao.impl.OrderDaoImpl;
import com.mock.dao.impl.OrderDetailImpl;
import com.mock.dao.impl.ProductDaoImpl;
import com.mock.dao.impl.ProductTypeDaoImpl;
import com.mock.dao.impl.UserDaoImpl;
import com.mock.model.Member;
import com.mock.model.OrderDetail;
import com.mock.model.OrderFood;
import com.mock.model.Product;
import com.mock.model.User;
import com.opensymphony.xwork2.ActionSupport;

public class updateOrderAction extends ActionSupport{
	private OrderFood orderFood;
	private List<Long> productIds;
	private List<String> quantitys;
	private Long memberId;
	private Long userId;
	private ProductDao productDao = new ProductDaoImpl();
	private MemberDao memberDao = new MemberDaoImpl();
	private UserDao userDao = new UserDaoImpl();
	private OrderDao orderDao = new OrderDaoImpl();
	private OrderDetailDao orderDetailDao = new OrderDetailImpl();
	public String execute(){
		Member member = memberDao.getById(Member.class, memberId);
		if(member == null){
			return ERROR;
		}
		orderFood.setMember(member);
		User user = userDao.getById(User.class, userId);
		if(user == null){
			return ERROR;
		}
		orderFood.setUser(user);
		List<OrderDetail> orderDetails = new ArrayList<>();
		
		if(productIds != null){
			for(int i = 0; i < productIds.size(); i++){
				Product product = productDao.getById(Product.class, productIds.get(i));
				OrderDetail orderDetail = new OrderDetail();
				orderDetail.setProduct(product);
				orderDetail.setQuantity(Integer.parseInt(quantitys.get(i)));
				orderDetail.setOrder(orderFood);
				/*orderDetailDao.add(orderDetail);*/
				orderDetails.add(orderDetail);
			}
		}
		orderFood.setOrderDetails(orderDetails);
		orderDao.add(orderFood);
		return SUCCESS;
	}
	public OrderFood getOrderFood() {
		return orderFood;
	}
	public void setOrderFood(OrderFood orderFood) {
		this.orderFood = orderFood;
	}
	
	public List<String> getQuantitys() {
		return quantitys;
	}
	public void setQuantitys(List<String> quantitys) {
		this.quantitys = quantitys;
	}
	public Long getMemberId() {
		return memberId;
	}
	public void setMemberId(Long memberId) {
		this.memberId = memberId;
	}
	public Long getUserId() {
		return userId;
	}
	public void setUserId(Long userId) {
		this.userId = userId;
	}
	public List<Long> getProductIds() {
		return productIds;
	}
	public void setProductIds(List<Long> productIds) {
		this.productIds = productIds;
	}
}
